import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:poet/components/poet_item.dart';
import 'package:poet/pages/search_page/author_page.dart';
import 'package:poet/pages/share_page/index.dart';
import 'components/item_group.dart';

class PoetPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PoetPage();
  }
}

class _PoetPage extends State<PoetPage> {
  final _contentTypes = ["正文", "注释", "译文", "赏析"];

  var _soundValue = 25.0;

  var _marked = false;

  void _onTypeChange(String type) {}

  Widget _buildSheetItem(String label, String b1, String b2) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Text(label),
        OutlineButton(
          child: Text(b1),
        ),
        OutlineButton(
          child: Text(b2),
        ),
      ],
    );
  }

  void _showFontConfig() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (context) {
        return Dialog(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                _buildSheetItem("字号", "-", "+"),
                _buildSheetItem("字体", "繁体", "更多字体"),
                _buildSheetItem("其他", "拼音", "夜间模式"),
              ],
            ),
          ),
        );
      },
    );
  }

  void _showSoundConfig() {
    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, state) {
            return Dialog(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        IconButton(
                          icon: Icon(Icons.fast_rewind),
                        ),
                        IconButton(
                          icon: Icon(Icons.pause),
                        ),
                        IconButton(
                          icon: Icon(Icons.fast_forward),
                        ),
                      ],
                    ),
                    Slider(
                      value: _soundValue,
                      label: '$_soundValue',
                      min: 0.0,
                      max: 100.0,
                      onChanged: (double) {
                        setState(() {
                          _soundValue = double;
                        });
                      },
                    ),
                  ],
                ),
              ),
            );
          },
        );
      },
    );
  }

  void _showSharePage() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => SharePage(),
      ),
    );
  }

  void _markPoet() {
    // 切换收藏状态
    setState(() {
      _marked = !_marked;
    });
    Fluttertoast.showToast(
      msg: "已${_marked ? '' : '取消'}收藏",
      gravity: ToastGravity.BOTTOM,
      textColor: Colors.white,
    );
  }

  Widget _buildMarkIcon() {
    return IconButton(
      icon: Icon(Icons.bookmark, color: _marked ? Colors.red[500] : null),
      onPressed: _markPoet,
    );
  }

  void _showAuthPage() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => AuthorPage(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(icon: Icon(Icons.format_size), onPressed: _showFontConfig),
          IconButton(icon: Icon(Icons.headset), onPressed: _showSoundConfig),
          _buildMarkIcon(),
          IconButton(icon: Icon(Icons.share), onPressed: _showSharePage),
        ],
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: PoetItem(onAuthorTap: _showAuthPage),
          ),
          ItemGroup(onItemChange: _onTypeChange, items: _contentTypes),
        ],
      ),
    );
  }
}
